package com.ete.service.service.log

import android.util.Log
import com.blankj.utilcode.util.ThreadUtils
import com.ete.service.manager.ManagerConstant
import com.ete.service.callback.log.IZipCallback
import net.lingala.zip4j.progress.ProgressMonitor

/**
 * Author:LiXian
 * <p>
 * Desc:压缩进度
 * Version:1.0
 * date 2024/1/8 14:53
 */
class ZipProgress(
    var fromZipPathList: MutableList<String>,
    var toZipPath: String,
    var mCallback: IZipCallback
) : ProgressMonitor() {

    // 每个目录的压缩都会回调0-100, 因此不能根据单个文件的进度来确定是否上传完成.
    // 需要根据文件数量*100作为总压缩进度来判断是否已经压缩完毕
    private var totalPercent = fromZipPathList.size * 100
    // 当前已压缩进度，每完成一个目录的压缩，加100进度, 当进度等于totalPercent总进度时, 说明压缩完毕
    private var curCompressPercent = 0

    override fun updateWorkCompleted(workCompleted: Long) {
        super.updateWorkCompleted(workCompleted)
        ThreadUtils.runOnUiThread {
            // 每个目录的压缩都会回调0-100， 需要根据已压缩的进度和总进度来计算真实的压缩进度
            mCallback.onProgress((((curCompressPercent + percentDone) / totalPercent.toFloat()) * 100).toInt())
        }
    }

    override fun endProgressMonitorSuccess() {
        super.endProgressMonitorSuccess()
        // 每压缩完一个目录, 进去加100, 当进度等于总进度时, 压缩完成
        curCompressPercent += 100
        if (curCompressPercent >= totalPercent){
            ThreadUtils.runOnUiThread {
                mCallback.onSuccess(fromZipPathList, toZipPath)
            }
        }
    }

    override fun endProgressMonitorError(e: Throwable?) {
        super.endProgressMonitorError(e)
        ThreadUtils.runOnUiThread {
            mCallback.onError(ManagerConstant.ERROR_ZIP_EXCEPTION, e?.message ?: "压缩异常")
        }
    }
}